package com.slkj.shilixiaoyuanapp.ui.parent.tool.leave;

import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;
import com.example.api.InjectParam;
import com.slkj.shilixiaoyuanapp.R;
import com.slkj.shilixiaoyuanapp.base.ActivityInfo;
import com.slkj.shilixiaoyuanapp.base.BaseActivity;
import com.slkj.shilixiaoyuanapp.data.LeaveTypeData;
import com.slkj.shilixiaoyuanapp.data.UserRequest;
import com.slkj.shilixiaoyuanapp.entity.parent.AskLeaveRecordEntity;
import com.slkj.shilixiaoyuanapp.net.HttpHeper;
import com.slkj.shilixiaoyuanapp.net.callback.CommonCallBack;
import com.slkj.shilixiaoyuanapp.ui.common.adapter.ChosePicAdapter;
import com.slkj.shilixiaoyuanapp.view.NumberEditText;
import com.slkj.shilixiaoyuanapp.view.dialog.LoadSuccessAndFailDialog;
import butterknife.BindView;
import butterknife.OnClick;
import static com.slkj.shilixiaoyuanapp.entity.code.UiCode.CHOSE_PIC_SPAN_COUNT;

@ActivityInfo(layout = R.layout.activity_leave_dsp1, title = "待审批")
public class LeaveDspActivity extends BaseActivity {
    @InjectParam
    int id;
    @InjectParam
    AskLeaveRecordEntity data;
    @BindView(R.id.recyc_pic)
    RecyclerView recycerPic;
    @BindView(R.id.tv_leavetype)
    TextView tvType;
    @BindView(R.id.tv_start_time)
    TextView tvStartTime;
    @BindView(R.id.tv_end_time)
    TextView tvEndTime;
    @BindView(R.id.edit_day)
    TextView editDay;
    @BindView(R.id.edit_content)
    NumberEditText editContent;
    @BindView(R.id.tv_student_name)
    TextView tvUserName;
    @BindView(R.id.tv_cb)
    TextView tvCb;
    ChosePicAdapter adapter;
    int classId=21;
    @Override
    protected void init() {
        LeaveDspActivity__JumpCenter.bind(this);
        tvUserName.setText(data.getName());
        if (data.getType()==1){
            tvType.setText("事假");
        }else {
            tvType.setText("病假");
        }
        tvStartTime.setText(data.getStartDate());
        tvEndTime.setText(data.getEndDate());
        editDay.setText(String.valueOf(data.getDuration()));
        editContent.setText(data.getBody());
        if (data.getImg()!= null && !data.getImg().isEmpty()){
            recycerPic.setLayoutManager(new GridLayoutManager(this, CHOSE_PIC_SPAN_COUNT));
            adapter = new ChosePicAdapter(data.getImg(), this,false);
            recycerPic.setAdapter(adapter);
        }
        tvCb.setEnabled(!data.isUrge());
    }
    /**
     * 撤销
     */
    @OnClick(R.id.tv_cx)
    void delete() {
        HttpHeper.get().parentService()
                .withDrawLeave(id)
                .compose(bindToLifecycle()).compose(getThread())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(LeaveDspActivity.this,data);
                        finish();
                    }
                });
    }

    /**
     * 催办
     */
    @OnClick(R.id.tv_cb)
    void cb() {
        for (int i = 0; i<UserRequest.getInstance().getUser().getStudents().size(); i++){
            if (UserRequest.getInstance().getUser().getStudents().get(i).getIsSelect()==1){
                classId= UserRequest.getInstance().getUser().getStudents().get(i).getClassId();
            }
        }
        HttpHeper.get().parentService()
                .CuiBan(classId,id)
                .compose(bindToLifecycle()).compose(getThread())
                .subscribe(new CommonCallBack<String>(true,this) {
                    @Override
                    public void onCallBackSuccess(String data) {
                        LoadSuccessAndFailDialog.showSuccess(LeaveDspActivity.this,data);
                        tvCb.setEnabled(false);
                    }
                });
    }

}
